﻿package main {
	import common.KeyCode;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import idv.cjcat.stardust.common.clocks.SteadyClock;
	import idv.cjcat.stardust.twoD.actions.DeathZone;
	import idv.cjcat.stardust.twoD.actions.Move;
	import idv.cjcat.stardust.twoD.emitters.Emitter2D;
	import idv.cjcat.stardust.twoD.initializers.DisplayObjectClass;
	import idv.cjcat.stardust.twoD.initializers.Position;
	import idv.cjcat.stardust.twoD.initializers.Velocity;
	import idv.cjcat.stardust.twoD.renderers.DisplayObjectRenderer;
	import idv.cjcat.stardust.twoD.zones.Line;
	import idv.cjcat.stardust.twoD.zones.RectZone;
	import idv.cjcat.stardust.twoD.zones.SinglePoint;
	import main.Wind;
	
	/**
	 * ...
	 * @author ...
	 */
	public class Yacht {
		[Embed(source = '../../lib/yacht04.png')]
		private static var YachtImage:Class;
		private static const OFFSET_Y:Number = -70;
		
		public function Yacht() {
			init();
			var w:int = 20; // ヨットの幅
			var h:int = 100; // ヨットの長さ = マストの幅
			var t:int = 4; // マストの厚さ
			
			var bitmap:Bitmap = new YachtImage();
			bitmap.scaleX = bitmap.scaleY = 0.5;
			bitmap.x = -bitmap.width / 2;
			bitmap.y = -bitmap.height / 2 + OFFSET_Y;
			_sprite = new Sprite();
			_sprite.addChild(bitmap);
			
			mast = new Mast(100);
			mast.getSprite().y = -30 + OFFSET_Y;
			mast.setAngle(20);
			_sprite.addChild(mast.getSprite());
			
			helm = new Helm();
			helm.getSprite().y = 120 + OFFSET_Y;
			helm.setAngle(10);
			_sprite.addChild(helm.getSprite());
			
			centerBoardForce = new Sprite();
			centerBoardForce.graphics.beginFill(0xFF0000);
			centerBoardForce.graphics.drawRect(0, -5, 50, 10);
			centerBoardForce.graphics.endFill();
			//_sprite.addChild(centerBoardForce);
			
			var renderer:DisplayObjectRenderer = new DisplayObjectRenderer(sprite);
			renderer.addEmitter(emitter);
			
			// http://blog.rainyday.jp/flash/pv3d/stardust01.html
			emitter.addInitializer(new DisplayObjectClass(Dot));
			emitter.addInitializer(new Position(
					new Line(-5, 120 + OFFSET_Y, 10, 120 + OFFSET_Y)));
			emitter.addInitializer(new Velocity(new RectZone(-2, 2, 4, 1)));
			
			var move:Move = new Move();
			var deathZone:DeathZone = new DeathZone(new RectZone(-100, 0, 200, 200), true);
			emitter.addAction(move);
			emitter.addAction(deathZone);
			
			sprite.addEventListener(Event.ENTER_FRAME, emitter.step);
		}
		
		public function init():void {
			x = y = angle = 0;
		}
		
		public function onKeyDown(keyCode:KeyCode):void {
			if (keyCode == KeyCode.LEFT) {
				mast.setAngle(mast.getAngle() + 5);
			} else if (keyCode == KeyCode.RIGHT) {
				mast.setAngle(mast.getAngle() - 5);
			}
			if (keyCode == KeyCode.UP) {
				helm.setAngle(helm.getAngle() + 5);
			} else if (keyCode == KeyCode.DOWN) {
				helm.setAngle(helm.getAngle() - 5);
			}
			sprite.rotation = -helm.getAngle();
		}
		
		private var _sprite:Sprite;
		public function get sprite():Sprite {
			return _sprite;
		}
		
		private var x:Number;
		public function getX():Number {
			return x;
		}
		
		private var y:Number;
		public function getY():Number {
			return y;
		}
		
		/**
		 * 向き in degree
		 * 正面が0度で、反時計回りが正。
		 */
		private var angle:Number;
		public function getAngle():Number {
			return angle;
		}
		
		public function getHelmAngle():Number {
			return helm.getAngle();
		}
		
		/** マスト */
		private var mast:Mast;
		/** 舵 */
		private var helm:Helm;
		/** センターボードによる力 */
		private var centerBoardForce:Sprite;
		
		private var emitter:Emitter2D = new Emitter2D(new SteadyClock(0.5));
		
		/**
		 * ヨットに風を当てます。
		 * @param	wind
		 */
		public function giveWind(wind:Wind):void {
			angle += helm.getAngle() / 20;
			x += -1 * Math.sin(angle * Math.PI / 180);
			y += -1 * Math.cos(angle * Math.PI / 180);
		}
		
	}
	
}

import flash.display.Sprite;
class Dot extends Sprite {
	public function Dot():void {
		graphics.lineStyle(1, 0xFFFFFF);
		graphics.moveTo( -10, 0);
		graphics.curveTo(0, 5, 10, 0);
	}
}
